package cn.sxt.seata.controller;

import cn.sxt.seata.entity.CommonResult;
import cn.sxt.seata.entity.Storage;
import cn.sxt.seata.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: --扣库存
 * @author：Bing
 * @date：2021/4/19 15:39
 * @version：1.0
 */
@RestController
public class StorageController {
    @Autowired
    private StorageService storageService;

    /**
     * 扣库存  懒人写法
     * @param productId
     * @param count
     * @return
     */
    @RequestMapping("storage/decrease")
    public CommonResult decrease(Long productId, Integer count) {
        Storage storage1 = storageService.selectByPrimaryKey(1l);
        Storage storage = Storage.builder().id(1l).productId(productId)
                .used(storage1.getUsed()+count)
                .residue(storage1.getResidue() - count).build();
        storageService.updateByPrimaryKeySelective(storage);
        return new CommonResult(200, "扣减库存成功----------------->");
    }
}
